package com.xjtu.duansheli.controller;


import com.xjtu.duansheli.entity.IdentifyImagesInfo;
import com.xjtu.duansheli.entity.ProductImagesInfo;
import com.xjtu.duansheli.entity.UserInfo;
import com.xjtu.duansheli.json.AjaxResponse;
import com.xjtu.duansheli.service.IdentifyImagesInfoService;
import com.xjtu.duansheli.service.IdentifyInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 漩涡鸣人
 * @since 2020-10-24
 */
@Slf4j
@RestController
@RequestMapping("/project/identify-images-info")
@Api(value = "认证图片信息管理模块", description = "这是认证图片信息增删改查的模块")
public class IdentifyImagesInfoController {

    @Autowired
    private IdentifyImagesInfoService idenImageInfoService;

    @ApiOperation(value = "获取全部认证图片", notes = "获取所有认证图片的信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current", value = "当前页", required = false, defaultValue = "1", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "size", value = "每页数量", required = false, defaultValue = "10", paramType = "query", dataType = "int")
    })
    @RequestMapping(value = "/getIdenImages", method = RequestMethod.GET)
    public AjaxResponse getAll(@RequestParam(value = "current") Integer current,
                               @RequestParam(value = "size") Integer size) {
        return AjaxResponse.success(idenImageInfoService.getAllIdentifyImages(current,size));
    }

    @ApiOperation(value = "根据id查找认证图片", notes = "获取根据id查找到的认证图片")
    @ApiImplicitParam(name = "id", value = "认证图片id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/getIdenImagesById", method = RequestMethod.GET)
    public AjaxResponse getIdenImageById(@RequestParam(name = "id") Long id) {
        return AjaxResponse.success(idenImageInfoService.getIdentifyImagesByid(id));
    }

    @ApiOperation(value = "根据用户id查找认证图片", notes = "获取根据用户id查找到的认证图片")
    @ApiImplicitParam(name = "uid", value = "用户id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/getIdenImagesByuId", method = RequestMethod.GET)
    public AjaxResponse getIdenImageByuId(@RequestParam(name = "uid") Long uid) {
        return AjaxResponse.success(idenImageInfoService.getIdentifyImagesByuid(uid));
    }

    @ApiOperation(value = "查找所有未经过审批的图片", notes = "获取所有未经过审批的图")
    @RequestMapping(value = "/getIdenImagesByIsIden", method = RequestMethod.GET)
    public AjaxResponse getIdenImageByIsIden() {
        return AjaxResponse.success(idenImageInfoService.getIdentifyImagesByIden());
    }

    @ApiOperation(value = "添加认证图片信息", notes = "获取认证图片信息并且添加")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "认证图片文件", required = true, paramType = "form", dataType = "file"),
    })
    @RequestMapping(value = "/addIdenImages", method = RequestMethod.POST)
    public AjaxResponse addIdenImage(@RequestParam(value = "file") MultipartFile file, HttpServletRequest request) {
        UserInfo loginUser = (UserInfo) request.getSession().getAttribute("loginUser");
        return AjaxResponse.success("数据库受影响行数->" + idenImageInfoService.addIdentifyImages(file, loginUser.getUid()));
    }

//    @ApiOperation(value = "修改认证图片信息", notes = "录入新的认证图片信息并且修改")
//    @ApiImplicitParams({
//            @ApiImplicitParam(name = "uName", value = "用户名", required = true, paramType = "form", dataType = "String"),
//            @ApiImplicitParam(name = "gender", value = "用户性别", required = true, paramType = "form", dataType = "String"),
//            @ApiImplicitParam(name = "wechat", value = "用户微信", required = true, paramType = "form", dataType = "String"),
//            @ApiImplicitParam(name = "phone", value = "用户手机号", required = true, paramType = "form", dataType = "String")
//    })
//    @RequestMapping(value = "/updateIdenImage", method = RequestMethod.POST)
//    public AjaxResponse updateIdenImage(@RequestParam(value = "uName") String uName,
//                                   @RequestParam(value = "gender") String gender,
//                                   @RequestParam(value = "wechat") String wechat,
//                                   @RequestParam(value = "phone") String phone) {
//
//        return AjaxResponse.success("数据库受影响行数->" + idenImageInfoService.updateIdentifyImages(user));
//    }

    @ApiOperation(value = "删除认证图片信息", notes = "从数据库中删除某个认证图片")
    @ApiImplicitParam(name = "id", value = "删除认证图片id", required = true, paramType = "query", dataType = "Long")
    @RequestMapping(value = "/deleteIdenImage", method = RequestMethod.DELETE)
    public AjaxResponse deleteIdenImage(@RequestParam(value = "id") Long id) {

        IdentifyImagesInfo identifyImagesInfo = idenImageInfoService.getIdentifyImagesByid(id);
        File myDelFile = new File(identifyImagesInfo.getImagePath());
        boolean delete = myDelFile.delete();
        log.info("delete file resource:" + identifyImagesInfo.getImagePath());
        if (delete)
            return AjaxResponse.success("数据库受影响行数->" + idenImageInfoService.deleteIdentifyImages(id));
        else
            return AjaxResponse.error("本地文件不存在或者删除失败！");
    }

    @ApiOperation(value = "认证图片总数量", notes = "获取数据库中认证图片总数量")
    @RequestMapping(value = "/getTotal", method = RequestMethod.GET)
    public AjaxResponse getTotal() {
        return AjaxResponse.success(idenImageInfoService.getTotal());
    }

}

